package com.example.learn.juc;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

public class AtomicExample8 {

    private static AtomicIntegerFieldUpdater<Teacher> integerFieldUpdater = AtomicIntegerFieldUpdater.newUpdater(Teacher.class, "ticketNum");

    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(1000);
        // 设置懵懂少年 180票
        Teacher teacher = new Teacher("懵懂少年", 180);


        ExecutorService executor = Executors.newFixedThreadPool(1000);
        for (int i = 0; i < 1000; i++) {
            executor.submit(() -> {
                for (int j = 0; j < 100; j++) {
                    integerFieldUpdater.incrementAndGet(teacher);

                }
                System.out.println("countDownLatch count is " + countDownLatch.getCount());
                countDownLatch.countDown();
            });
        }
        System.out.println("before countDownLatch count is " + countDownLatch.getCount());

        countDownLatch.await();
        System.out.println("after countDownLatch count is " + countDownLatch.getCount());

        System.out.println(teacher.getTicketNum());
        executor.shutdown();
    }
}